package game;

import game.GameEvent;
import game.GameClient;

import java.util.ArrayList;

public class Game {
	
	private ArrayList<GameClient> clients;
	private ArrayList<GameEvent> events;
	
	
	
	public Game() {
		
		this.clients = new ArrayList<GameClient>();
		this.events = new ArrayList<GameEvent>();
	}
				
	public GameEvent getEvent(GameClient client) {
		
		for(int i = 0; i < this.events.size(); i++) {
			
			GameEvent evt = this.events.get(i);
			
			if(this.events.get(i).getRecpId() == client.getId()) {
				
				this.events.remove(evt);
				return evt; 
			
			}
			
		}
		return null;
	}
	
	public GameEvent createEvent(String from, String type, String data) {
		
		GameEvent ownEvent = new GameEvent(from, from, type, data);
		this.events.add(ownEvent);
		for(int i = 0; i < this.clients.size(); i++) {
			
			if(from != this.clients.get(i).getId()) {
			
				this.events.add( new GameEvent(from, this.clients.get(i).getId(), type, data));
			}
		}
		
		return ownEvent;		
	}
	
	
	public GameClient createClient(String request) {
		
		GameClient client = new  GameClient(this, request);
		this.clients.add(client);
		return client;
	}
	
	public void removeClient(GameClient client) {
		
		this.clients.remove(client);
	}
}
